package com.hqyj.springboot.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.springboot.modules.common.vo.Result;
import com.hqyj.springboot.modules.common.vo.Search;
import com.hqyj.springboot.modules.test.dao.CityDao;
import com.hqyj.springboot.modules.test.dao.CityDao2;
import com.hqyj.springboot.modules.test.entity.City;
import com.hqyj.springboot.modules.test.service.CityService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityDao cityDao;
    @Autowired
    private CityDao2 cityDao2;

    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if(temp != null){
            return new Result<>(Result.ResultStatus.FAILD.code, "City name is repead.");
        }
            city.setDateCreated(new Date());
            city.setDateModified(new Date());
//            cityDao.insertCity(city);

            cityDao2.insert(city);
            return new Result<>(Result.ResultStatus.SUCCESS.code, "Insert success.",city);

    }

    @Override
    public Result<City> updateCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if(temp != null && !temp.getCityId().equals(city.getCityId())){
            return new Result<>(Result.ResultStatus.FAILD.code, "City name is repead.");
        }
            city.setDateModified(new Date());
//            cityDao.updateCity(city);
//        会修改所以字段
//            cityDao2.updateById(city);
//            指定字段名进行修改
            UpdateWrapper wrapper = new UpdateWrapper();

            wrapper.set(true,"city_name",city.getCityName());
            wrapper.set(true,"local_city_name",city.getLocalCityName());
            wrapper.eq(true,"city_id",city.getCityId());
            cityDao2.update(city,wrapper);

            return new Result<>(Result.ResultStatus.SUCCESS.code, "update success .",city);


    }

    @Override
    public Result<Object> deleteCityByCityId(int cityId) {
//        cityDao.deleteCityByCityId(cityId);
//        cityDao2.deleteById(cityId);
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("city_id",cityId);
        cityDao2.deleteByMap(columnMap);
        return new Result<>(Result.ResultStatus.SUCCESS.code, "delete success .");
    }

    @Override
    public City getCityByCityId(int cityId) {

//        return cityDao.getCityByCityId(cityId);

        return cityDao2.selectById(cityId);
    }

    @Override
    public City getCityByNames(String cityName, String localCityName) {
        return cityDao.getCityByNames(cityName,localCityName);
    }

    @Override
    public List<City> getCitiesByCountryId(int countryId) {
//        return cityDao.getCitiesByCountryId(countryId);
      return   Optional.ofNullable(cityDao.getCitiesByCountryId(countryId))
                .orElse(Collections.emptyList());
    }

    @Override
    public PageInfo<City> getCitiesBySearch(Search search) {
        //1.初始化search对象
        search.init();
        //2.开始分页
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        //3.返回分页对象
        return new PageInfo<>(Optional
                .ofNullable(cityDao.getCotiesBySearch(search))
                .orElse(Collections.emptyList()));
    }

    @Override
    public Page<City> getCitiesBySearch2(Search search) {
//       //构造page对象
//        Page<City> page = new Page<>(search.getCurrentPage(),search.getPageSize());
//       //构造QueryWrapper对象
//        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(search.getKeyword()),
//                "city_name",
//                search.getKeyword()
//
//        ).or().like(StringUtils.isNotBlank(search.getKeyword()),
//                "local_city_name",
//                search.getKeyword()
//
//        );
//
//        queryWrapper.orderBy(true,
//                StringUtils.isBlank(search.getDirection()) ||
//                      search.getDirection().toLowerCase().equals("asc"),
//                      search.getSort());
//         //查询
//       return cityDao2.selectPage(page,queryWrapper);
        search.init();
       Page page = new Page(search.getCurrentPage(),search.getPageSize());
       QueryWrapper<City> queryWrapper = new QueryWrapper();
       queryWrapper.like(StringUtils.isNotBlank(search.getKeyword()),
               "city_name",
               search.getKeyword()).or().like(StringUtils.isNotBlank(search.getKeyword()),
               "local_city_name",
               search.getKeyword()).orderBy(true,
               StringUtils.isBlank(search.getSort()) ||
               search.getSort().toLowerCase().equals("asc"),
               search.getSort());
      return   cityDao2.selectPage(page,queryWrapper);
    }
}
